package com.lanou.remember.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.k12.entity.Question;
import com.lanou.k12.entity.Record;
import com.lanou.k12.entity.ResultObject;
import com.lanou.k12.entity.User;
import com.lanou.remember.mapper.QuestionMapper;
import com.lanou.remember.mapper.RecordMapper;
import com.lanou.remember.service.IQuestionService;
import com.lanou.remember.utils.GetLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class QuestionService implements IQuestionService {

    @Autowired
    QuestionMapper mapper;
    @Autowired
    RecordMapper recordMapper;

    @Override
    public ResultObject findProcess() {
        ResultObject obj = new ResultObject();
        HashMap<String,Integer> process = new HashMap<>();
        User u = GetLoginUser.getLoginUser();
        //调用已有接口查询学习题数
        List<Record>  list = recordMapper.findRecord(u.getId());
        process.put("已学习", list.size());
        process.put("题库总题数", mapper.findQuestionNum());
        obj.setStatus(1);
        obj.setData(process);
        if(list == null) {
            obj.setInfo("没开始学习");
            return obj;
        }
        return obj;
    }

    @Override
    public ResultObject addQuestion(Question question) {
        ResultObject obj = new ResultObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String date = sdf.format(new Date());
        question.setDate(date);
        question.setUid(GetLoginUser.getLoginUser().getId());
        obj.setStatus(mapper.addQuestion(question));
        return obj;
    }

    @Override
    public ResultObject findQuestionByKeyWord(Integer id, String question, String answer) {
        ResultObject obj = new ResultObject();
        List<Question> list = mapper.findQuestionByKeyWord(id, question, answer);
        if (list == null) {
            obj.setStatus(0);
            obj.setInfo("没有这个题");
        } else {
            obj.setStatus(1);
            obj.setData(list);
        }
        return obj;
    }

    @Override
    public ResultObject findQuestion(int page, int size) {
        ResultObject obj = new ResultObject();
        int record = recordMapper.findUser(GetLoginUser.getLoginUser().getId());
        if (record == 0) {
            obj.setStatus(1);
            obj.setInfo("第一次学习，加油！");
            PageHelper.startPage(page,size);
            PageInfo<Question> info = new PageInfo<>(mapper.findFirstQuestion());
            obj.setData(info);
            return obj;
        }
        PageHelper.startPage(page,size);
        PageInfo<Question> info = new PageInfo<>(mapper.findQuestion(record));
        if (info.getSize() == 0) {
            obj.setStatus(0);
            obj.setInfo("没了");
        } else {
            obj.setStatus(1);
            obj.setData(info);
        }
        return obj;
    }

    @Override
    public ResultObject updateQuestion(Question question) {
        ResultObject obj = new ResultObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String date = sdf.format(new Date());
        question.setDate(date);
        question.setUid(GetLoginUser.getLoginUser().getId());
        obj.setStatus(mapper.updateQuestion(question));
        return obj;
    }

    @Override
    public ResultObject findQuestionByStatus(int page, int size) {
        ResultObject obj = new ResultObject();
        PageHelper.startPage(page,size);
        PageInfo<Question> info = new PageInfo<>(mapper.findQuestionByStatus());
        if (info.getSize() == 0) {
            obj.setStatus(0);
            obj.setInfo("没有待审核任务");
        } else {
            obj.setStatus(1);
            obj.setData(info);
        }
        return obj;
    }

    @Override
    public ResultObject deleteQuestion(int qid) {
        ResultObject obj = new ResultObject();
        int result = mapper.deleteQuestion(qid,1-mapper.findQuestionStatus(qid));
        obj.setStatus(result);
        return obj;
    }


}
